import { Server } from "../core/server";
import { Dispatcher } from "../tools/Dispatcher";
import { ProtoIDs } from "../core/ProtoIDs";

import Constants from "../Contants";

import { DataManager } from "../data/DataManager";

import TouchCtrl from "./TouchCtrl";
import DispatcherEvent from "../tools/DispatcherEvent";


export class GameCtrl {

    private static _install: GameCtrl;

    public static get instance() {
        if (this._install == null) {
            this._install = new GameCtrl();
        }
        return this._install;
    }
   
    private _rootView: fgui.GComponent;


    public init() {
        this.initUI();
        this.addListener();

    }

    private initUI() {
        fgui.UIConfig.packageFileExtension = "txt";
        fgui.UIConfig.modalLayerColor = "rgba(0,0,0,0.7)";
        fgui.UIPackage.loadPackage(Constants.FguiPackageName, Laya.Handler.create(this, this.complete));
    }


    private complete() {
        this.initData();
     
        fgui.UIPackage.addPackage(Constants.FguiPackageName);
        Laya.stage.addChild(fgui.GRoot.inst.displayObject);
        var rootView: fgui.GComponent = fairygui.UIPackage.createObject("AirPlane", "Main").asCom;
        fairygui.GRoot.inst.addChildAt(rootView, 0);
        rootView.width = Laya.stage.width;
        rootView.height = Laya.stage.height;
        TouchCtrl.instance.init(Laya.stage);
        Dispatcher.dispatch(DispatcherEvent.CAN_TOUCH, true);

       
        this._rootView = rootView;
      

       
    }

    private initData() {
       
    }

    private addListener() {
        Dispatcher.addListener(ProtoIDs.loginMsgID.S2C_Login, this, this.S2C_Login);


        //Laya.stage.on(Laya.Event.KEY_DOWN, this, this.onClickKeyBtn);
    }

  
   
   

  



    private S2C_Login(data: protobuf.S2C_Login) {
      
    }

    private S2C_LoginOut(s2c_loginOut: protobuf.S2C_LoginOut) {
       
    }

  

    private mouseX: number;
    private mouseY: number;
    private _isMouseDown: boolean;
    public mouseDown(x: number, y: number) {
        this.mouseX = x;
        this.mouseY = y;
        this._isMouseDown = true;
    }

    public mouseUp(x: number, y: number) {
        this._isMouseDown = false;
    }

    public mouseMove(x: number, y: number) {
        let angle = Math.atan2(x - this.mouseX, y - this.mouseY)
      
        this.mouseX = x;
        this.mouseY = y;
    }

}